package com.fit.piris.dao.fanpage;

import java.util.List;

import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.fit.piris.entities.fanpage.FanPage;
import com.fit.piris.model.StringReplacment;

public class FanPageDAOImpl implements FanPageDAO{

	@Inject
	private Session session;
	
	@Log
	public boolean save(FanPage fanPage) {
		if (fanPage!=null) {
			session.save(fanPage);
			return true;
		}
		return false;
	}

	public boolean delete(FanPage fanPage) {
		if (fanPage!=null) {
			session.delete(fanPage);
			return true;
		}
		return false;
	}

	public boolean update(FanPage fanPage) {
		if (fanPage!=null) {
			session.update(fanPage);
			return true;
		}
		return false;
	}

	public boolean deleteByID(Long id) {
		FanPage f = findByID(id);
		if (f!=null) {
			session.delete(f);
			return true;
		}
		return false;
	}

	public FanPage findByID(Long id) {
		return (FanPage) session.get(FanPage.class, id);
	}

	public List<FanPage> findAll() {
		return session.createCriteria(FanPage.class).addOrder(Order.asc("name")).list();
	}

	public List<FanPage> findByName(String name) {
		return session.createCriteria(FanPage.class).add(Restrictions.like("name", StringReplacment.forWhere(name)
				)).addOrder(Order.asc("name")).list();
	}

	
	
}
